package com.artron.mediaartron.ui.fragment.order;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.entity.Invoice;
import com.artron.mediaartron.ui.activity.GeneralActivity;

import butterknife.BindView;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-27.
 */

public class InvoiceFragment extends BaseStaticFragment {

    @BindView(R.id.InvoiceFragment_rb_not)
    protected RadioButton mRbNot;
    @BindView(R.id.InvoiceFragment_rb_yes)
    protected RadioButton mRbYes;
    @BindView(R.id.InvoiceFragment_et_content)
    protected EditText mEtContent;
    @BindView(R.id.InvoiceFragment_rb_detail)
    protected RadioButton mRbDetail;
    @BindView(R.id.InvoiceFragment_btn_save)
    protected Button mBtnSave;
    @BindView(R.id.InvoiceFragment_fl_container)
    protected FrameLayout mFlContainer;
    private Invoice mInvoice;

    public static InvoiceFragment newInstance(Invoice invoice) {
        InvoiceFragment fragment = new InvoiceFragment();
        fragment.init(invoice);
        return fragment;
    }

    private void init(Invoice invoice) {
        this.mInvoice = invoice;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_invoice;
    }

    @Override
    protected void initView() {
        if (mInvoice != null) {
            mEtContent.setText(mInvoice.getTitle());
            mRbYes.setChecked(true);
        }

        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRbYes.isChecked()) {
                    Invoice invoice = new Invoice();
                    String text = mEtContent.getText().toString();
                    if (!TextUtils.isEmpty(text)) {
                        invoice.setTitle(text);
                        Intent intent = new Intent();
                        intent.putExtra(GeneralActivity.CONTENT, invoice);
                        getActivity().setResult(GeneralActivity.RESULT_OK, intent);
                    }
                }
                getActivity().onBackPressed();
            }
        });

        mFlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtils.hiddenKeyboard(getView());
            }
        });
    }
}
